package cn.nohan.funplay.module.member.mongo.config;

import cn.hutool.core.date.DateUtil;
import com.mongoplus.mapping.MongoConverter;
import com.mongoplus.strategy.conversion.ConversionStrategy;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author caozhipeng
 * @date 2025/7/22
 */
@Slf4j
@Component
public class MongoDBDateConversionStrategy implements ConversionStrategy<Date> {

    @Override
    public Date convertValue(Object fieldValue, Class<?> fieldType, MongoConverter mongoConverter) throws IllegalAccessException {
        Date date = null;
        if (fieldValue.getClass().equals(Long.class)){
            date = new Date((Long) fieldValue);
        }else if(fieldValue.getClass().equals(Date.class)){
            date = (Date) fieldValue;
        } else if (fieldValue.getClass().equals(Document.class)) {
            Long timestamp = ((Document) fieldValue).getLong("fastTime");
            if(timestamp == null){
                return null;
            }
            date = new Date(timestamp);;
        }
        return date;
    }


}
